package com.weixing.mall.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.core.support.auth.AgentAuth;
import com.weixing.mall.provider.api.model.vo.AgentAuthVo;
import com.weixing.mall.provider.api.service.AgentServiceRpc;
import com.weixing.mall.provider.enums.MsgFlagEnum;
import com.weixing.oauth2.common.userdetails.IUserAuthService;
import com.weixing.oauth2.common.userdetails.SecurityUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Date;

/**
 * 代理商登录实现类
 *
 * @author  siashan
 * @since  v1.0
 **/
@Component("agentAuthService")
@Slf4j
public class AgentAuthServiceImpl implements IUserAuthService {
    @Autowired
    private AgentServiceRpc agentServiceRpc;

    @Override
    public SecurityUser loadUserByUsername(String loginName) {
        Result<AgentAuthVo> agentVoResult = agentServiceRpc.selectAuthVoByLoginName(loginName);
        return getSecurityUser(agentVoResult);
    }

    @Override
    public SecurityUser loadUserByOpenId(String openId) {
        return null;
    }

    @Override
    public SecurityUser loadUserByXcxOpenId(String openId) {
        return null;
    }

    private SecurityUser getSecurityUser(Result<AgentAuthVo> voResult) {
        SecurityUser securityUser = new SecurityUser();
        if (!voResult.isSuccess()) {
            securityUser.setMsgFlag(MsgFlagEnum.EXCEPTION.getValue());
        } else {
            AgentAuthVo authVo = voResult.getData();
            if (null != authVo) {

                int compare = DateUtil.compare(new Date(), authVo.getMaturityTime());
                if (compare > 0 || IsEnableEnum.DISABLE.getValue().equals(authVo.getIsEnable())) {
                    securityUser.setEnable(false);
                }
                securityUser.setMsgFlag(MsgFlagEnum.SUCCESS.getValue());
                securityUser.setLoginName(authVo.getMobile());
                securityUser.setLoginPwd(authVo.getPassword());
                securityUser.setUserRole(GlobalConstant.UserRole.AGENT);
                securityUser.setSensitiveData(wrapperSenExt(authVo));
            } else {
                securityUser.setMsgFlag(MsgFlagEnum.FAIL.getValue());
            }
        }
        return securityUser;
    }


    private AgentAuth wrapperSenExt(AgentAuthVo authVo) {
        AgentAuth ext = new AgentAuth();
        ext.setAid(authVo.getId());
        ext.setAName(authVo.getAgentName());
        ext.setASn(authVo.getAgentSn());
        ext.setACline(authVo.getAgentTreePath());
        ext.setKid(authVo.getCustomerId());
        ext.setKname(authVo.getCustomerName());
        ext.setLockDate(authVo.getLockDate());
        ext.setDealer(authVo.getDealer());
        ext.setDealerId(authVo.getDealerId());
        ext.setAlevel(authVo.getAgentLevel());
        ext.setIsAds(authVo.getIsAdsAuth());
        ext.setAdsNum(authVo.getAdsLimit());
        ext.setAType(authVo.getAgentType());
        return ext;
    }

    @Override
    public SecurityUser loadUserByMobile(String mobile) {
//        Result<MemberAuthVo> voResult = memberServiceRpc.loadUserByMobile(mobile);
//        return getSecurityUser(voResult);
        return null;
    }

    @Override
    public Collection<GrantedAuthority> loadAuthority(Long userId) {
        return null;
    }

    @Override
    public UserDetails loadUserByPhoneAndPassword(String phone, String password) {
        return null;
    }

    @Override
    public UserDetails loadUserByPhoneAndSmsCode(String phone, String smsCode) {
        return null;
    }

    @Override
    public void handlerLoginData(HttpServletRequest request, SecurityUser securityUser) {

    }
}
